// Window.cpp : implementation file
//

#include "stdafx.h"
#include "ansieditor.h"
#include "Window.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// Window

IMPLEMENT_OLETYPELIB(Window, LIBID_ANSIEDITOR, _wVerMajor, _wVerMinor)

IMPLEMENT_DYNCREATE(Window, CCmdTargetPlus)

BEGIN_INTERFACE_MAP(Window, CCmdTargetPlus)
	INTERFACE_PART(Window, IID_IWindow, Dispatch)
END_INTERFACE_MAP()

BEGIN_DISPATCH_MAP(Window, CCmdTargetPlus)
	//{{AFX_DISPATCH_MAP(Window)
	DISP_PROPERTY_EX(Window, "CaretX", GetCaretX, SetCaretX, VT_I2)
	DISP_PROPERTY_EX(Window, "CaretY", GetCaretY, SetCaretY, VT_I4)
	DISP_FUNCTION(Window, "SetTimeout", SetTimeout, VT_I4, VTS_I4)
	DISP_FUNCTION(Window, "ClearTimeout", ClearTimeout, VT_EMPTY, VTS_I4)
	DISP_FUNCTION(Window, "MoveCaret", MoveCaret, VT_BOOL, VTS_I2 VTS_I2)
	DISP_FUNCTION(Window, "MessageBox", MessageBox, VT_I4, VTS_BSTR VTS_BSTR VTS_I4)
	//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()

BEGIN_EVENT_MAP(Window, CCmdTargetPlus)
	//{{AFX_EVENT_MAP(Window)
	EVENT_CUSTOM("OnTimeout", FireOnTimeout, VTS_I4)
	EVENT_CUSTOM("OnEventTest", FireOnEventTest, VTS_NONE)
	//}}AFX_EVENT_MAP
END_EVENT_MAP()

BEGIN_MESSAGE_MAP(Window, CCmdTargetPlus)
	//{{AFX_MSG_MAP(Window)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


Window::Window()
{
	m_piidPrimary = &IID_IWindow;
	m_piidEvents = &IID_IWindowEvents;
}

Window::~Window()
{
}

HRESULT Window::GetClassID(LPCLSID pclsid)
{
	*pclsid = CLSID_Window;
	return NOERROR;
}

void Window::OnFinalRelease()
{
	CCmdTargetPlus::OnFinalRelease();
}


/////////////////////////////////////////////////////////////////////////////
// Window message handlers

VARIANT_BOOL Window::MoveCaret(short nCaretX, short nCaretY) 
{
	int x = nCaretX - 1;
	int y = nCaretY - 1;
	if(x < 0 || x > MAX_COL - 1 || y < 0 || y > MAX_ROW - 1)
	{
		return FALSE;
	}
	CPoint pt(x, y);
	GetAppContext()->m_pView->SetCaretPos(pt);
	return TRUE;
}

VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
{
	gpApp->m_xApplication.m_xWindow.FireOnTimeout(idEvent);
}

long Window::SetTimeout(long nMilliSeconds) 
{
	return ::SetTimer(NULL, 0, nMilliSeconds, TimerProc);
	return 0;
}

void Window::ClearTimeout(long nTimerID) 
{
	KillTimer(NULL, nTimerID);
}

short Window::GetCaretX() 
{
	return (short)(GetAppContext()->m_pView->GetCaretPos().x + 1);
}

void Window::SetCaretX(short nNewValue) 
{
	--nNewValue;
	if(nNewValue > MAX_COL - 1)
	{
		nNewValue = 0;
	}
	CPoint ptCaret(GetAppContext()->m_pView->GetCaretPos());
	ptCaret.x = nNewValue;
	GetAppContext()->m_pView->SetCaretPos(ptCaret);
}

long Window::GetCaretY() 
{
	return GetAppContext()->m_pView->GetCaretPos().y + 1;
}

void Window::SetCaretY(long nNewValue) 
{
	--nNewValue;
	if(nNewValue > MAX_ROW - 1)
	{
		nNewValue = 0;
	}
	CPoint ptCaret(GetAppContext()->m_pView->GetCaretPos());
	ptCaret.y = nNewValue;
	GetAppContext()->m_pView->SetCaretPos(ptCaret);
}

long Window::MessageBox(LPCTSTR Text, LPCTSTR Caption, long nType) 
{
	return AfxGetMainWnd()->MessageBox(Text, Caption, nType);
}
